package view;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.graphics.PixelFormat;
import android.opengl.GLSurfaceView;
import android.opengl.GLU;
import android.util.AttributeSet;

import com.dmapp.utils.cHighTimer;


public class GLView extends GLSurfaceView{  

	
	// http://blog.csdn.net/zhangyadick18/article/details/7457733
	// 使能在xml中引用
	 public GLView(Context context, AttributeSet attrs) { 
	        super(context, attrs); 
	        
	       init(context);
			
	    } 
	
	
	public GLView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		
		init(context);
		
	}
	
	void init(Context context){
		

		/* OK  使透明
		   // We want an 8888 pixel format because that's required for
  // a translucent window.
  // And we want a depth buffer.
  this.setEGLConfigChooser(8, 8, 8, 8, 16, 0);

  
    cube_render = new CubeRenderer(true);
    this.setRenderer(cube_render);
  	
    // Tell the cube renderer that we want to render a translucent version
    // of the cube:
    // Use a surface format with an Alpha channel:
    this.getHolder().setFormat(PixelFormat.TRANSLUCENT);
    */
		
		
		  
	    cube_render = new CubeRenderer(true);
	    this.setRenderer(cube_render);
	    
	
	}
	
	
	
	private  GLRender render;
	
	private CubeRenderer cube_render;
	
}


class GLRender implements GLSurfaceView.Renderer{

   private static final String TAG = "GLRenderer";
   private final Context m_context;
   
   private FloatBuffer m_VertexBuff;
   
   
   private cHighTimer HighTimer = new cHighTimer();

   
   
   private final GLText text = new GLText();
   
   
   
   GLRender(Context context){
	   this.m_context =context;
	   
	   
		HighTimer.start_counter();
   }
	
	@Override
	public void onSurfaceCreated(GL10 gl, javax.microedition.khronos.egl.EGLConfig config) {
		// TODO Auto-generated method stub
		
		//   .
		// .   .
		float[] point_array ={
				0, 0.5f, 0.0f,
				-0.5f, 0, 0.0f,
				0.5f, 0, 0.0f
		};
		
		
		ByteBuffer vbb =ByteBuffer.allocateDirect(point_array.length * 4);
		vbb.order(ByteOrder.nativeOrder());
		this.m_VertexBuff =vbb.asFloatBuffer();
		m_VertexBuff.put(point_array);
		m_VertexBuff.position(0); //定位到最前端
		
		
		// 初始化输出文字
		text.loadStr(gl, "OPENGL");
		
	}

	@Override
	public void onSurfaceChanged(GL10 gl, int width, int height) {
		// TODO Auto-generated method stub
		
		gl.glViewport(0, 0, width, height);
		gl.glMatrixMode(GL10.GL_PROJECTION);
		GLU.gluPerspective(gl, 45, (float)width/height, 1.0f, 100.0f);
				
	}

	@Override
	public void onDrawFrame(GL10 gl) {
		// TODO Auto-generated method stub
		
		sniffer(gl);
		
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT);    // gl.glClearColor(12, 12, 12, 0.2f);
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glLoadIdentity();
		gl.glTranslatef(0, 0, -3.0f);
		
		rota(gl);
		
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, this.m_VertexBuff);
		
		gl.glEnable(gl.GL_ALPHA);
		gl.glColor4f(1, 1, 1, 0.1f);
		gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);

	
	
	//	text.draw(gl);
		
	}


	private void  sniffer(GL10 gl){
		
		float elapsed =HighTimer.get_elapsed_time();
		float fps = HighTimer.get_fps(elapsed);
		
	//	String str ="Frames per second: " + fps + " (" + 
    //           " frames in " + elapsed + " ms)";
		/*
		 Log.d(TAG, "Frames per second: " + fps + " (" + 
	                " frames in " + elapsed + " ms)");
		 */
		
		
		
	}
	

	private void rota(GL10 gl){
		
		RotaAngle +=1f;
		
		if(RotaAngle > 360.0f)
			RotaAngle =0;
		
		
		
		gl.glRotatef(RotaAngle, 0, 1, 0);
		
	}
	

	
	float RotaAngle =0;
	
	
	
}



